package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.study.entity.EduQuarter;
import com.xmy.study.service.IEduQuarterService;
import com.xmy.study.util.Code;
import com.xmy.study.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 每年季度 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-05-31
 */
@RestController
@RequestMapping("/api/admin/eduQuarter")
public class EduQuarterController {
    @Autowired
    IEduQuarterService eduQuarterService;

    @GetMapping("/quarterYear")
    @ApiOperation("根据当前年份遍历季度")
    public Result categoryYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        QueryWrapper wrapper = new QueryWrapper<>();
        Date date = new Date();
        wrapper.eq("year_part", sdf.format(date));
        List<EduQuarter> eduQuarters = eduQuarterService.list(wrapper);
        return new Result(Code.GET_OK, eduQuarters, Code.GET_OK_MSG);
    }

    @GetMapping("/quarterDate")
    @ApiOperation("根据当前日期获取季度")
    public Result categoryToday() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.le("start_date", LocalDate.now());
        wrapper.ge("end_date", LocalDate.now());
        EduQuarter eduQuarter = eduQuarterService.getOne(wrapper);
        return new Result(Code.GET_OK, eduQuarter, Code.GET_OK_MSG);
    }

    @PostMapping("/quarter")
    @ApiOperation("添加季度")
    public Result saveQuarter(@RequestBody EduQuarter eduQuarter) {
        Boolean save = eduQuarterService.save(eduQuarter);
        return new Result(save ? Code.SAVE_OK : Code.SAVE_ERROR, null, save ? Code.SAVE_OK_MSG : Code.SAVE_ERROR_MSG);
    }

    @GetMapping("/quarterByYear")
    @ApiOperation("根据年份条件获取季度")
    public Result categoryByYear(@RequestParam Map reMap) {
        String yearPart = (String) reMap.get("yearPart");
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("year_part", yearPart);
        List<EduQuarter> eduQuarters = eduQuarterService.list(wrapper);
        return new Result(Code.GET_OK, eduQuarters, Code.GET_OK_MSG);
    }
}
